package com.langyiquan.front.controller;

import com.langyiquan.exception.http.ParamException;
import com.langyiquan.pojo.ItemImg;
import com.langyiquan.pojo.ItemSpec;
import com.langyiquan.service.ItemService;
import com.langyiquan.utils.PagedGridResult;
import com.langyiquan.utils.UnifyResponse;
import com.langyiquan.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(value = "商品接口", tags = {"商品的相关接口"})
@RestController
@RequestMapping("items")
public class ItemController extends BaseController {

    @Autowired
    private ItemService itemService;

    @ApiOperation(value = "获取商品详情",notes = "获取商品详情",httpMethod = "GET")
    @GetMapping("/info/{itemId}")
    public UnifyResponse info(@ApiParam(name = "itemId",value = "商品Id", required = true) @PathVariable String itemId){

        if(itemId == null){
            throw new ParamException(5001);
        }
        ItemBaseInfoVO itemBaseInfoVO = itemService.queryItemById(itemId);
        List<ItemImg> itemImgs = itemService.queryItemImgsList(itemId);
        List<ItemSpec> itemSpecs = itemService.queryItemSpecsList(itemId);

        ItemInfoVO itemInfoVO = new ItemInfoVO();
        itemInfoVO.setItemBaseInfoVO(itemBaseInfoVO);
        itemInfoVO.setItemImgs(itemImgs);
        ItemSpecInfo itemSpecInfo = new ItemSpecInfo();
        itemSpecInfo.setItemSpecs(itemSpecs);
        itemSpecInfo.setSpecKey(itemSpecs.get(0).getSpecKey());
        List list = new ArrayList();
        list.add(itemSpecInfo);
        itemInfoVO.setItemSpecInfo(list);
        return UnifyResponse.success(itemInfoVO);

    }


    @ApiOperation(value = "查询商品评价等级",notes = "查询商品评价等级",httpMethod = "GET")
    @GetMapping("/commentLevel")
    public UnifyResponse commentLevel(@ApiParam(name = "itemId",value = "商品Id", required = true) @RequestParam String itemId){

        if(itemId == null){
            throw new ParamException(5001);
        }

        CommentLevelCountVO commentLevelCountVO = itemService.queryCommentCounts(itemId);
        return UnifyResponse.success(commentLevelCountVO);

    }


    @ApiOperation(value = "查询商品评论",notes = "查询商品评论",httpMethod = "GET")
    @GetMapping("/comments")
    public UnifyResponse comments(@RequestParam String itemId,@RequestParam Integer level,@RequestParam Integer page,@RequestParam Integer pageSize){

        if(itemId == null){
            throw new ParamException(5001);
        }

        PagedGridResult pagedGridResult = itemService.queryPagedComments(itemId,level,page,pageSize);
        return UnifyResponse.success(pagedGridResult);

    }


    @ApiOperation(value = "搜索商品列表",notes = "搜索商品列表",httpMethod = "GET")
    @GetMapping("/search")
    public UnifyResponse search(@RequestParam String keywords,@RequestParam String sort,@RequestParam Integer page,@RequestParam Integer pageSize){

        PagedGridResult pagedGridResult = itemService.searchItems(keywords,sort,page,pageSize);
        return UnifyResponse.success(pagedGridResult);

    }

    @ApiOperation(value = "ES搜索商品列表",notes = "ES搜索商品列表",httpMethod = "GET")
    @GetMapping("/es-search")
    public UnifyResponse esSearch(@RequestParam String keywords,@RequestParam String sort,@RequestParam Integer page,@RequestParam Integer pageSize){
        page--;
        PagedGridResult pagedGridResult = itemService.esSearchItems(keywords,sort,page,pageSize);
        return UnifyResponse.success(pagedGridResult);

    }


}
